<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item> <i class="el-icon-lx-cascades"></i> 商品分类 </el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <div class="handle-box">
                <el-input
                    v-model="query.title"
                    placeholder="分类名称"
                    class="handle-input mr10"
                    @keyup.enter.native="handleSearch"
                ></el-input>
                <el-button type="primary" icon="el-icon-search" @click="handleSearch">搜索</el-button>
                <el-button type="primary" icon="el-icon-plus" @click="handleEdit()">新增</el-button>
            </div>
            <el-table
                v-loading="loading"
                :data="tableData"
                border
                class="table"
                ref="multipleTable"
                header-cell-class-name="table-header"
                row-key="id"
                lazy
                :load="load"
                :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
            >
                <el-table-column label="分类名称" prop="title">
                    <template slot-scope="scope">
                        <el-tooltip v-if="scope.row.type == 1" :content="scope.row.icon" placement="top">
                            <i :class="scope.row.icon"></i>
                        </el-tooltip>

                        <span style="margin-left: 10px">{{ scope.row.title }}</span>
                    </template>
                </el-table-column>
                <el-table-column prop="parentId" width='0px'  cell-style="color:red;"  style="color:red;" label="父级"></el-table-column>
                <el-table-column prop="sort" label="排序(降序)"></el-table-column>
                <el-table-column prop="remark" label="备注"></el-table-column>
                <el-table-column label="操作" width="180" align="center">
                    <template slot-scope="scope">
                        <el-button type="text" icon="el-icon-edit" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
                        <el-button type="text" icon="el-icon-delete" class="red" @click="handleDelete(scope.$index, scope.row)"
                            >删除</el-button
                        >
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination">
                <el-pagination
                    @size-change="handleSizeChange"
                    @current-change="handleCurrentChange"
                    :current-page="1"
                    :page-sizes="[10, 15, 20, 30]"
                    :page-size="10"
                    layout="total, sizes, prev, pager, next, jumper"
                    :total="pageTotal"
                >
                </el-pagination>
            </div>
        </div>

        <!-- 编辑弹出框 -->
        <el-dialog :close-on-click-modal="false" title="编辑内容" :visible.sync="editVisible">
            <el-form ref="form" :model="form" label-width="100px" :rules="rulesForm">
                <el-form-item label="类别名称" prop="title">
                    <el-input v-model="form.title" id="title"></el-input>
                </el-form-item>

                <el-form-item label="上级分类">
                    <el-select
                        v-model="form.parentId"
                        clearable
                        filterable
                        placeholder="(可选)"
                        id="parentId"
                        @clear="clearSelect"
                        @change="menuChange"
                    >
                        <el-option
                            v-bind:style="item.parentId != '' ? 'padding-left:30px;' : ''"
                            v-for="item in selectCat"
                            :key="item.id"
                            :label="item.title"
                            :value="item.id"
                        >
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="展示顺序" prop="sort">
                    <el-input v-model="form.sort" id="sort" type="number"></el-input>
                </el-form-item>
                <el-form-item label="备注信息" prop="remark">
                    <el-input id="remark" type="textarea" :rows="3" placeholder="不超过50个字" v-model="form.remark"></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="resetForm('form')">取 消</el-button>
                <el-button type="primary" @click="saveEdit('form')">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
import axios from 'axios';
export default {
    name: 'meun_list',
    data() {
        return {
			dis:{

				display:"none"
			}
			,
            query: {
                title: '',
                type: 0,
                pageIndex: 1,
                pageSize: 10
            },
            tableData: [],
            selectCat: [],
            multipleSelection: [],
            delList: [],
            editVisible: false,
            pageTotal: 0,
            form: {},
            loading: false,
            parentId: '',
            editTitle: '新增类别',
            rulesForm: {
                title: [
                    {
                        required: true,
                        message: '请输入名称!'
                    },
                    {
                        min: 2,
                        max: 20,
                        message: '长度在 2 到 20 个字符'
                    }
                ],
                sort: [
                    {
                        required: true,
                        message: '展示顺序不能为空!'
                    }
                ],
                memo: [
                    {
                        min: 0,
                        max: 50,
                        message: '长度不能超过50个字符'
                    }
                ]
            }
        };
    },
    created() {
        this.loading = true;
        //config.gatewayUrl = "http://localhost:52296";

        axios({
            url: config.gatewayUrl + '/api/product/cat/paged',
            method: 'post',
            data: {
                parentId: 0,
                type: 1,
                pageIndex: 1,
                pageSize: 500
            },
            headers: {
                // 'Content-Type': 'multipart/form-data'
            }
        }).then((res) => {
            this.selectCat = res.data.items;
        });

        this.getData();
    },
    methods: {
        // 触发搜索按钮
        handleSearch() {
            this.query.pageIndex = 1;
            this.pageTotal = 0;
            this.tableData = [];
            this.getData();
        },
        getData() {
            this.loading = true;
            axios({
                url: config.gatewayUrl + '/api/product/cat/paged',
                method: 'post',
                data: {
                    parentId: 0,
                    title: this.query.title,
                    type: 1,
                    pageIndex: this.query.pageIndex,
                    pageSize: this.query.pageSize
                },
                headers: {
                    // 'Content-Type': 'multipart/form-data'
                }
            }).then((res) => {
                this.tableData = res.data.items;
                this.pageTotal = res.data.totalCount;
                this.loading = false;
            });
        },
        load(tree, treeNode, resolve) {
            axios
                .post(config.gatewayUrl + '/api/product/cat/paged', {
                    parentId: tree.id
                })
                .then((res) => {
                    if (res.data.totalCount == 0) resolve([]);
                    else resolve(res.data.items);
                });
        },
        // 删除操作
        handleDelete(index, row) {
            // 二次确认删除
            var that = this;
            this.$confirm('确定要删除【' + row.title + '】吗？', '提示', {
                type: 'warning'
            })
                .then(() => {
                    axios.get(config.gatewayUrl + '/com/category/delete/' + row.id).then((res) => {
                        that.getData();
                        that.tableData.splice(index, 1);
                        that.$message.success('删除成功');
                    });
                })
                .catch(() => {});
        },
        menuChange(val) {
            var item = this.selectCat.filter(function (item) {
                return item.id == val;
            });

            this.form.parentName = item[0].title;
            this.form.parentId = val;
        },
        clearSelect() {
            this.form.parentName = '';
            this.form.parentId = '';
        },
        // 编辑操作
        handleEdit(index, row) {
            if (row) {
                this.idx = index;
                this.form = row;
                this.form.parentId = row.parentId == 0 ? '' : row.parentId;

                //this.form.parentName=row.parentId;
            } else {
                this.form = {
                    id: 0,
                    title: '',
                    parentId: '',
                    remark: '',
                    sort: 100
                };
            }
            this.editVisible = true;
        },
        // 保存编辑
        saveEdit(formName) {
            var that = this;
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    var dto = this.form;
                    console.log(dto);

                    var m = that.selectCat.filter(function (item) {
                        return item.id == dto.parentId;
                    })[0];
                    dto.type = 1;
                    if (dto.parentId == '') {
                        dto.parentId = 0;
                    }
                    axios
                        .post(config.gatewayUrl + '/api/product/cat/create', dto)
                        .then((res) => {
                            that.editVisible = false;
                            that.$message.success(`保存成功!`);
                            that.getData();
                        })
                        .catch((error) => {
                            console.log(error); //  错误处理 相当于error
                        });
                } else {
                    return false;
                }
            });
        },
        resetForm(formName) {
            this.form = {
                id: '',
                title: '',
                parentId: '',
                parentId: '',
                remark: '',
                sort: 100,
            };
            this.editVisible = false;
            this.$refs[formName].resetFields();
        },
        // 分页导航
        handlePageChange(val) {
            this.$set(this.query, 'pageIndex', val);
            this.getData();
        },
        handleSizeChange(val) {
            this.query.pageSize = val;
            this.getData();
        },
        handleCurrentChange(val) {
            //console.log(`当前页: ${val}`);

            this.query.pageIndex = val;
            this.getData();
        }
    }
};
</script>

<style scoped>
.dd {
    display: none !important;
}
.handle-box {
    margin-bottom: 20px;
}

.handle-select {
    width: 120px;
}

.handle-input {
    width: 300px;
    display: inline-block;
}

.table {
    width: 100%;
    font-size: 14px;
}

.red {
    color: #ff0000;
}

.mr10 {
    margin-right: 10px;
}

.table-td-thumb {
    display: block;
    margin: auto;
    width: 40px;
    height: 40px;
}
</style>
